package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbp.dao.AlipayConfigDao;
import com.jbp.model.AlipayConfig;
import com.jbp.service.AlipayConfigService;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * (AlipayConfig)表服务实现类
 *
 * @author makejava
 * @since 2021-01-25 00:01:42
 */
@Service
public class AlipayConfigServiceImpl extends ServiceImpl<AlipayConfigDao, AlipayConfig> implements AlipayConfigService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<AlipayConfig> queryWrapper = new QueryWrapper<>();
        if (params.get("id") != null) {
            queryWrapper.like("id", params.get("id"));
        }
        IPage<AlipayConfig> page = this.page(
                new Query<AlipayConfig>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public AlipayConfig getAlipayAccount(Integer money) {
        AlipayConfig alipayConfig = getOne(new QueryWrapper<AlipayConfig>()
                .eq("del", 0)
                .eq("money", money));
        if (alipayConfig != null) {
            return alipayConfig;
        }
        List<AlipayConfig> alipayConfigs = list(new QueryWrapper<AlipayConfig>()
                .eq("del", 0)
                .orderByAsc("sort"));
        if (alipayConfigs != null && alipayConfigs.size() > 0){
            return alipayConfigs.get(0);
        } else {
            return null;
        }
    }
}